package com.equipment_management_p2.service.Impl;


import com.equipment_management_p2.mapper.EquipmentMapper;
import com.equipment_management_p2.service.IEquipmentService;
import com.pojo.Entity.Equipment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class EquipmentServiceImpl implements IEquipmentService {

    @Autowired
    EquipmentMapper equipmentMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public Integer insert(Equipment equipment) {
        return equipmentMapper.insert(equipment);
    }

    @Override
    public Integer modifyQuantity(Integer eID, Integer eSum,Integer userID) {
        Integer sum=(Integer) redisTemplate.opsForHash().get(String.valueOf(userID),"By:"+String.valueOf(eID));
        if(sum==null)
            sum=0;
       if(sum+eSum>10){
           return 0;
       }

       redisTemplate.opsForHash().put(String.valueOf(userID),"By:"+String.valueOf(eID),sum+eSum);
        return equipmentMapper.modifyQuantity(eID,eSum);
    }

}
